package org.esisar.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.esisar.controller.utils.SessionHelper;
import org.esisar.dao.JDBCDAO;
import org.esisar.dao.PictureStatementCreator;
import org.esisar.model.Model;
import org.esisar.model.Picture;

public class PhotoDisplayController extends HttpServlet {

	JDBCDAO jdbcDao = new JDBCDAO();

	public void doGet(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException{



		String id = String.valueOf(SessionHelper.getUserId(req));

		if (req.getParameter("role") != null){
			try {
				List<Model> photos = jdbcDao.findAll(new PictureStatementCreator(), "idClient", id);

				req.setAttribute("photos", photos);

				RequestDispatcher dispatcher = req.getRequestDispatcher("/Photo/getPhotos.jsp");
				dispatcher.forward(req, res);

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else{
			String pictureId = req.getParameter("image");

			Picture picture;
			try {
				picture = (Picture) jdbcDao.find(new PictureStatementCreator(), "idPicture", pictureId);

				req.setAttribute("photo", picture);

				RequestDispatcher dispatcher = req.getRequestDispatcher("/Photo/displayPhoto.jsp");
				dispatcher.forward(req, res);

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
